package com.bnlr.lrparking.appui.mine.collection.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.stall.StallBegRentBuy;
import com.bnlr.lrparking.appui.stall.StallDealDetailActivity;
import com.bnlr.lrparking.adapter.CarPlaceCollectAdapter;
import com.bnlr.lrparking.base.BaseFragment;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.StallListBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.widget.refresh.RefreshHolderUtil;
import com.bnlr.lrparking.widget.refresh.XRefreshLayout;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 车位收藏页面
 *
 * A simple {@link Fragment} subclass.
 * @author tian
 */
public class CarPlaceCollectFragment extends BaseFragment {

    private  int currentPage = 1;
    private Unbinder unbinder;
    @BindView(R.id.rl_place_refresh)
    XRefreshLayout swipeRefreshLayout;
    @BindView(R.id.lv_place_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;
    @BindView(R.id.tv_hint_tips)
    TextView tvHint;
    @BindView(R.id.iv_hint)
    ImageView ivHint;
    private String type = "0";
    private CarPlaceCollectAdapter adapter;
    private List<StallListBean.DataBean> dataList = new ArrayList<>();
    public  void newInstance(String param2) {
        type = param2;
        currentPage = 1;
        getCollectData(param2,currentPage);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_car_place_collect, container, false);
        unbinder = ButterKnife.bind(this,view);
        initView();
        tvHint.setText("暂无车位收藏信息");
        ivHint.setImageResource(R.mipmap.nocollection);
        return view;
    }
    private void initView() {
       // getCollectData(type,currentPage);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        swipeRefreshLayout.setRefreshViewHolder(RefreshHolderUtil.getRefreshHolder(getActivity()));
        swipeRefreshLayout.setDelegate(new XRefreshLayout.BGARefreshLayoutDelegate() {
            @Override
            public void onBGARefreshLayoutBeginRefreshing(XRefreshLayout refreshLayout) {
                currentPage = 1;
                getCollectData(type,currentPage);
            }

            @Override
            public boolean onBGARefreshLayoutBeginLoadingMore(XRefreshLayout refreshLayout) {
                return false;
            }

        });
        //swipeRefreshLayout.beginRefreshing();

        adapter = new CarPlaceCollectAdapter(dataList);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                //  上拉加载
                currentPage++;
                getCollectData(type,currentPage);
            }
        },mRecyclerView);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                StallListBean.DataBean md = dataList.get(position);
                Intent intent;
                if (md.getType() == 10 || md.getType() == 11) {
                    intent = new Intent(getActivity(), StallDealDetailActivity.class);
                } else {
                    intent = new Intent(getActivity(), StallBegRentBuy.class);
                }
                intent.putExtra("stall_id", md.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        currentPage = 1;
        getCollectData(type,currentPage);
    }

    private void getCollectData (String type , final int page) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "GetFavouriteParkSpace");
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("pageIndex",page);
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("type",type);

            // 添加到请求队列+
            CallServer.getRequestInstance().add(getActivity(), mRequest,
                    new CustomHttpListener<StallListBean>(getActivity(), true, StallListBean.class) {
                        @Override
                        public void doWork(StallListBean data, String code) {
                            if (TextUtils.equals("1", code)) {
                                if (adapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        llHint.setVisibility(View.GONE);
                                        if (page == 1) {
                                            dataList = data.getData();
                                            adapter.setNewData(data.getData());
                                        } else {
                                            dataList.addAll(data.getData());
                                            adapter.notifyDataSetChanged();
                                        }
                                        adapter.loadMoreComplete();
                                    } else {
                                        if (page == 1) {
                                            dataList.clear();
                                            adapter.notifyDataSetChanged();
                                            llHint.setVisibility(View.VISIBLE);
                                        } else {
                                            adapter.loadMoreEnd();
                                        }
                                    }
                                }

                            } else {
                                ToastUtils.showShort(data.getMsg()+"");
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            if (swipeRefreshLayout != null) {
                                swipeRefreshLayout.endRefreshing();
                            }
                        }
                    }, false);
        } catch (Exception e) {
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
